﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dominio;

namespace Persistencia
{
    class BrokerProveedor: Broker
    {
        #region constructor

        public BrokerProveedor()
        {
            this.conn = ConexionBD.getInstance();
        }

        #endregion

        #region methods

        public override void add(Object o)
        {            
            Proveedor proveedorAux = (Proveedor)o;
            this.conn.connect();
            string sql = "insert into Personas(Nombre, Direccion, IdPlan, Habilitado, Fax, EMail," 
                + " Saldo, EsCliente, RUT) values(" 
                + "'" + proveedorAux.Nombre + "', '" + proveedorAux.Direccion + "', " 
                + proveedorAux.PlanDePago.OID + ", " + proveedorAux.Habilitado + ", " + proveedorAux.Fax + ", '" 
                + proveedorAux.EMail + "', '" + proveedorAux.Saldo + "', false, '" + proveedorAux.RUT + "')";
            this.conn.execute(sql);  
            this.conn.disconnect();
        }

        public override void update(Object o)
        {
            Proveedor proveedorAux = (Proveedor)o;
            string sql = "update Personas set Nombre = '" + proveedorAux.Nombre
                + "', Direccion = '" + proveedorAux.Direccion
                + "', IdPlan = " + proveedorAux.PlanDePago.OID
                + ", Habilitado = " + proveedorAux.Habilitado
                + ", Fax = " + proveedorAux.Fax
                + ", EMail = '" + proveedorAux.EMail
                + "', Saldo = '" + proveedorAux.Saldo
                + "', RUT = '" + proveedorAux.RUT
                + "' where IdPersona = " + proveedorAux.OID;
            this.conn.connect();
            this.conn.execute(sql);            
            this.conn.disconnect();
        }

        public override void delete(Object o)
        {
            Proveedor proveedorAux = (Proveedor)o;
            string sql = "update Personas set Habilitado = false where idPersona = " + proveedorAux.OID; 
            this.conn.connect();
            this.conn.execute(sql);            
            this.conn.disconnect();
        }
        
        public override void bringData(Object o)
        {            
            Proveedor proveedorAux = (Proveedor)o;
            this.conn.connect();
            string sql = "Select * from Personas" 
                + " where IdPersona = " + proveedorAux.OID;
            this.conn.bringData(sql);
            while (this.conn.nextRecord())
            {                
                proveedorAux.Nombre = this.conn.getDataString("Nombre").ToString();
                proveedorAux.Direccion = this.conn.getDataString("Direccion").ToString();
                proveedorAux.PlanDePago.OID = int.Parse(this.conn.getDataString("IdPlan").ToString());
                proveedorAux.Habilitado = (Boolean)this.conn.getDataString("Habilitado");
                proveedorAux.Fax = int.Parse(this.conn.getDataString("Fax").ToString());
                proveedorAux.EMail = this.conn.getDataString("EMail").ToString();
                proveedorAux.Saldo = double.Parse(this.conn.getDataString("Saldo").ToString());
                proveedorAux.RUT = this.conn.getDataString("RUT").ToString();
            }                        
            this.conn.disconnect();            
        }
        
        public override List<Object> bringList(Object o)
        {
            string sql = "Select * from Personas where EsCliente = false";
            List<Object> list = new List<Object>();
            this.conn.connect();
            this.conn.bringList(sql);
            while (this.conn.nextRecord())
            {
                Proveedor proveedorAux = new Proveedor();
                proveedorAux.Nombre = this.conn.getDataString("Nombre").ToString();
                proveedorAux.Direccion = this.conn.getDataString("Direccion").ToString();
                proveedorAux.PlanDePago.OID = int.Parse(this.conn.getDataString("IdPlan").ToString());
                proveedorAux.Habilitado = (Boolean)this.conn.getDataString("Habilitado");
                proveedorAux.Fax = int.Parse(this.conn.getDataString("Fax").ToString());
                proveedorAux.EMail = this.conn.getDataString("EMail").ToString();
                proveedorAux.Saldo = double.Parse(this.conn.getDataString("Saldo").ToString());
                proveedorAux.OID = int.Parse(this.conn.getDataString("IdPersona").ToString());
                proveedorAux.RUT = this.conn.getDataString("RUT").ToString();
                list.Add(proveedorAux);
            }            
            this.conn.disconnect();
            return list;
        }

        public override int bringLastId()
        {
            int id = 0;
            try
            {
                this.conn.connect();
                string sql = "Select max(idPersona) as idPersona from Personas";
                this.conn.bringData(sql);
                while (this.conn.nextRecord())
                {
                    id = int.Parse(this.conn.getDataString("idPersona").ToString());
                }
                this.conn.disconnect();
            }
            catch (Exception)
            {
                this.conn.disconnect();
                id = 0;
            }            
            return id;
        }

        #endregion        
    }
}
